package cz.hackathon;

import cz.hackathon.core.QrReader;
import cz.hackathon.rateit.model.Action;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final Button btnQR = (Button) findViewById(R.id.btn_scan_qr);
		btnQR.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Log.d("QRButton", "intent to qrcode");
				Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		        intent.setPackage("com.google.zxing.client.android");
		        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		        startActivityForResult(intent, 0);
			}
		});

	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	    if (requestCode == 0) {
	        if (resultCode == RESULT_OK) {
	            String contents = intent.getStringExtra("SCAN_RESULT");
	            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	            Log.d("contents", contents);
	            Log.d("format", format);

	            QrReader reader = new QrReader();
	            try {
					Action action = reader.getAction(contents);
//					intent = new Intent(getBaseContext(), ItemsActivity.class);
					intent = new Intent(getBaseContext(), TabActivity.class);
					intent.putExtra("Action", action);
					startActivity(intent);				
					
				} catch (Exception e) {
					Log.d("","exceptiona :-)");
					// :-) help yourself
				}

	            // Handle successful scan
	        } else if (resultCode == RESULT_CANCELED) {
	            // Handle cancel
	        }
	    }
	}

}